extern crate winapi;

use winapi::um::winuser::{
    ShowWindow, SW_MAXIMIZE, SW_RESTORE, GetForegroundWindow, SW_SHOWMAXIMIZED,
};

fn main() {
    unsafe {
        let hwnd = GetForegroundWindow();
        if hwnd.is_null() {
            eprintln!("can't find window");
            return;
        }

        // get window state
        let mut placement: winapi::um::winuser::WINDOWPLACEMENT = std::mem::zeroed();
        placement.length = std::mem::size_of::<winapi::um::winuser::WINDOWPLACEMENT>() as u32;
        if winapi::um::winuser::GetWindowPlacement(hwnd, &mut placement) == 0 {
            eprintln!("get windows state fail");
            return;
        }

        if placement.showCmd as u32 == SW_SHOWMAXIMIZED as u32 {
            // if state is maxmized, restore it
            ShowWindow(hwnd, SW_RESTORE);
        } else {
            // otherwise, maxmize it
            ShowWindow(hwnd, SW_MAXIMIZE);
        }
    }
}
